package com.qyer.lxp.service.task;

import com.qyer.lxp.common.framework.core.BaseSpringService;
import com.qyer.lxp.common.framework.threadpool.AbstractAroundAdviceCallable;
import com.qyer.lxp.common.framework.threadpool.CommonThreadPool;
import com.qyer.lxp.model.errorlog.TaskErrorLog;
import com.qyer.lxp.service.errorlog.ITaskErrorLogService;
import com.qyer.lxp.service.price.IPriceService;
import com.qyer.lxp.service.product.IProductService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiangping on 16/6/3.
 */
@Component
public class ErrorLogTaskService extends BaseSpringService {

    @Resource
    private ITaskErrorLogService taskErrorLogService;

    @Resource
    private IProductService productService;

    @Resource
    private IPriceService priceService;

    public void startErrorLogTask(){
        while (true){
            TaskErrorLog taskErrorLog = new TaskErrorLog();
            taskErrorLog.setMethodName("updateBatchPickDrop");
            List<TaskErrorLog> paramList = taskErrorLogService.setlectTaskErrorLog(taskErrorLog);
            if(null != paramList && paramList.size() > 0){
                final List<String> productCodeList = new ArrayList<String>();
                for(TaskErrorLog procutCode:paramList){
                    productCodeList.add(procutCode.getParam());
                }
                CommonThreadPool.execute(new AbstractAroundAdviceCallable() {
                        @Override
                        public Object call() throws Exception {
                        productService.updateBatchPickDrop(productCodeList);
                        return null;
                    }
                });
                List<Integer> ids = new ArrayList<Integer>();
                for(TaskErrorLog procutCode:paramList){
                    ids.add(procutCode.getId());
                }
                taskErrorLogService.deleteBacthByPrimaryKey(ids);
            }

            TaskErrorLog taskErrorLogPrice = new TaskErrorLog();
            taskErrorLogPrice.setMethodName("updateBatchPrice");
            List<TaskErrorLog> paramPriceList = taskErrorLogService.setlectTaskErrorLog(taskErrorLogPrice);
            if(null != paramPriceList && paramPriceList.size() > 0){
                final List<String> productCodeList = new ArrayList<String>();
                for(TaskErrorLog procutCode:paramPriceList){
                    productCodeList.add(procutCode.getParam());
                }
                CommonThreadPool.execute(new AbstractAroundAdviceCallable() {
                    @Override
                    public Object call() throws Exception {
                        priceService.updateBatchPrice(productCodeList);
                        return null;
                    }
                });
                List<Integer> ids = new ArrayList<Integer>();
                for(TaskErrorLog procutCode:paramPriceList){
                    ids.add(procutCode.getId());
                }
                taskErrorLogService.deleteBacthByPrimaryKey(ids);
            }
            if((null == paramList || paramList.size() <= 0) && (null == paramPriceList || paramPriceList.size() <= 0)){
                break;
            }
        }
    }
}
